package handler

import (
	"crypto/md5"
	"encoding/hex"
	"log"

	"github.com/labstack/echo"
	cron "github.com/robfig/cron/v3"
)

//接口
type Res struct {
	Msg    string      `json:"msg"`
	Code   int         `json:"code"`
	Result interface{} `json:"result"`
}

//加密
func encypt(id string) (str string) {
	var s = "asdassad412as165a74sdas8as" + id
	h := md5.New()
	h.Write([]byte(s))
	return hex.EncodeToString(h.Sum(nil))
}

//token验证
func VerifyCookie(c echo.Context) (flag bool, err error) {
	flag = true
	token, err1 := c.Cookie("token")
	ids, err2 := c.Cookie("id")
	if err1 != nil || err2 != nil || encypt(ids.Value) != token.Value {
		flag = false
	}
	return
}

//定时清理图片
func SetTimeOutClearImg() {
	crontab := cron.New()
	// 添加定时任务, * * * * * 是 crontab,表示每分钟执行一次
	_, err := crontab.AddFunc("@daily", DelUselessImg)
	if err != nil {
		log.Println("定时任务开启失败")
		return
	}
	// 启动定时器
	crontab.Start()
	log.Println("定时任务开启")
	/* c.Stop()  // 停止定时任务(不停止已经运行的任务) */
}

//调用脚本同步到cdn
func SyncToCdn() (err error) {
	/* c := exec.Command("./rsync.sh")
	stdout, err := c.StdoutPipe()
	c.Stderr = c.Stdout
	err = c.Start()
	if err != nil {
		// todo 报警
		log.Println("SyncToCdn.Start()失败 err=", err.Error())
		return err
	}
	log.Println("SyncToCdn stdout:", stdout)
	err = c.Wait()
	if err != nil {
		return
	} */
	return
}
